/**
 * 
 */
package arisufascion.com.activity;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import arisufascion.com.AbstractActivity;
import arisufascion.com.R;
import arisufascion.com.capcha.Captcha;
import arisufascion.com.constant.WriteLog;
import arisufascion.com.network.RequestTask;

/**
 * @author Kelvin
 * 
 */
public class ActivityFeedback extends AbstractActivity {

	private TextView txt_message_success;
	private boolean isLoading = true;
	private ProgressDialog dialog;
	private EditText  editTextEmail;
	private Button btnSend;
	private String  email;
	private int code=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mailing_list);
		
		initView();
	
		

		findViewById(R.id.btn_menu).setOnClickListener(onClick);
		findViewById(R.id.btn_privacy).setOnClickListener(onClick);
		findViewById(R.id.btn_wholesale).setOnClickListener(onClick);
		findViewById(R.id.btn_shipping).setOnClickListener(onClick);

		
		findViewById(R.id.btnSend).setOnClickListener(onClickEvent);
	}

	public void initView() {
		editTextEmail = (EditText) findViewById(R.id.editTextEmail);
		btnSend = (Button) findViewById(R.id.btnSend);
		btnSend.setOnClickListener(onClick);
		txt_message_success = (TextView) findViewById(R.id.txt_message_success);
		findViewById(R.id.submit_form).setVisibility(VISIBLE);
		txt_message_success.setVisibility(GONE);
		
	}

	
	boolean isEmailValid(CharSequence email) {
		   return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
		}
	
	public OnClickListener onClickEvent = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnSend:

				email = editTextEmail.getText().toString();
				if(email.length()>0){
					if(!isEmailValid(editTextEmail.getText())){
						Spanned Mail_is_not_Valid = Html
								.fromHtml("<font color='red'>Mail is not Valid" + "</font>");			
								editTextEmail.setError(Mail_is_not_Valid);
								return;	
					}
				}else{
					Spanned Mail_is_not_Valid = Html
							.fromHtml("<font color='red'>Mail is not Valid" + "</font>");			
							editTextEmail.setError(Mail_is_not_Valid);
							return;	
				}
				
				new submitEmailling().execute();

				break;

			default:
				break;
			}
		}
	};

	public class submitEmailling extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
				dialog = new ProgressDialog(getParent());
				dialog.setMessage("Fetching data...");
				dialog.show();

		}

		@Override
		protected Boolean doInBackground(Void... params) {

			try {
				String json = RequestTask.createEmail(self,email);
				JSONObject jsonObj = new JSONObject(json);
				
				boolean success = jsonObj.getBoolean("success");
				code=jsonObj.getInt("code");
				
				WriteLog.d(TAG, jsonObj.getString("success"));
				
				return success;
			} catch (Exception e) {

			}
			return null;

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
			if(result!=null){
				//resetAllField();
				if(code==999){
					txt_message_success.setText("(MailChimp) "+editTextEmail.getText().toString()+" is already subscribed to list ArisuFascion Mailing List.");
				}else{
					txt_message_success.setVisibility(VISIBLE);
					txt_message_success.setText("Dear "+editTextEmail.getText().toString()+", Thank you for your submission.");
					findViewById(R.id.submit_form).setVisibility(GONE);
				}
				
			}
		}

	}

	public void resetAllField(){
		editTextEmail.setText("");
	}
}
